# -*- coding = utf-8 -*-
# @Time : 2023/5/29 13:29
# @Author :Henry L
# @File : HB.py
import glob
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


class HB:
    def __init__(self):
        options = webdriver.ChromeOptions()
        self.host = "http://www.hbyxjzcg.com/"
        prefs = {"plugins.always_open_pdf_externally": True,
                 "download.default_directory": os.getcwd() + "\\"}
        options.add_experimental_option("prefs", prefs)
        self.url = 'http://www.hbyxjzcg.com/index.html'
        self.driver = webdriver.Chrome(options=options)
        self.driver.maximize_window()
        self.wait = WebDriverWait(driver=self.driver, timeout=10)
        self.driver.get(self.url)

    def get(self, page):
        time.sleep(3)
        div1 = self.driver.find_element(By.XPATH, "/html/body/div[1]/div[6]/div[1]/div[2]")
        div2 = self.driver.find_element(By.XPATH, "/html/body/div[1]/div[6]/div[2]/div[2]")
        for div in [div1, div2]:
            lan = div.find_elements(By.XPATH, "ul[1]/li/a")
            date_list = div.find_elements(By.CLASS_NAME, "date")
            for item in range(0, len(lan)):
                time.sleep(3)
                # if item.find_element(By.XPATH, "div[1]/div[2]").text == '公告标题':
                #     continue
                date = date_list[item].text
                lan[item].click()
                handles = self.driver.window_handles
                self.driver.switch_to.window(handles[-1])
                time.sleep(1.5)
                title = self.driver.find_element(By.TAG_NAME, "h1").text
                box = self.driver.find_element(By.CLASS_NAME, "entry")
                view_time = ''
                content = box.get_attribute("outerHTML")
                attachments = box.find_elements(By.TAG_NAME, "a")
                attachments_list = []
                if len(attachments) > 0:
                    for a in attachments:
                        if a.text == "" or a.get_attribute("href") is None:
                            continue
                        if "upload" in a.get_attribute("href") or "附件" in a.text:
                            self.driver.execute_script("window.scroll(100,1000)")
                            a.click()
                            time.sleep(1)
                            # href = a.get_attribute("href").split('.')[0]
                            # a_name = href.split('/')[-1]
                            # attachments_list.append(a_name)
                            # self.get_last_filename_and_rename(os.getcwd(), a_name)
                time.sleep(1)
                self.driver.close()
                handles = self.driver.window_handles
                self.driver.switch_to.window(handles[-1])
                data_list = [title, date, view_time, content, attachments_list]
                print(data_list)

    def get_last_filename_and_rename(self, sav_folder, new_filename):
        files = glob.glob(sav_folder + '/*')
        max_file = max(files, key=os.path.getctime)
        filename = max_file.split("/")[-1].split(".")[0]
        new_path = max_file.replace(filename, new_filename)
        os.rename(max_file, new_path)
        return new_path

    def close(self):
        time.sleep(60)
        self.driver.quit()

hb = HB()
hb.get("")
hb.close()
